﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.ApplicationModel.Composition;
using System.ComponentModel.Composition.Hosting;

namespace Anagramme.Views
{
    /// <summary>
    /// Logique d'interaction pour GameView.xaml
    /// </summary>
    [ViewExport(Modules.Game)]
    [PartCreationPolicy(System.ComponentModel.Composition.CreationPolicy.NonShared)]
    public partial class GameView : UserControl
    {
        private List<Tuple<TextBox, Button>> _opponentWordsControls;
        private int _selectedIndex;
        private bool _newRound;

        /// <summary>
        /// Crée une nouvelle instance de la classe <see cref="GameView"/>
        /// </summary>
        public GameView()
        {
            InitializeComponent();

            this._opponentWordsControls = new List<Tuple<TextBox, Button>>();
            this._opponentWordsControls.Add(new Tuple<TextBox, Button>(this.OpponentWord1TextBox, this.OpponentWord1Button));
            this._opponentWordsControls.Add(new Tuple<TextBox, Button>(this.OpponentWord2TextBox, this.OpponentWord2Button));
            this._opponentWordsControls.Add(new Tuple<TextBox, Button>(this.OpponentWord3TextBox, this.OpponentWord3Button));
            this._opponentWordsControls.Add(new Tuple<TextBox, Button>(this.OpponentWord4TextBox, this.OpponentWord4Button));
            this._opponentWordsControls.Add(new Tuple<TextBox, Button>(this.OpponentWord5TextBox, this.OpponentWord5Button));

            DependencyPropertyDescriptor textTextblockDpd = DependencyPropertyDescriptor.FromProperty(TextBlock.TextProperty, typeof(TextBlock));
            textTextblockDpd.AddValueChanged(this.textRound, (s,e) =>
                {
                    this._selectedIndex = 0;
                    this._newRound = true;
                    this.ComputeButtonsVisibilityAndFocus();
                });

            DependencyPropertyDescriptor isEnabledButtonDpd = DependencyPropertyDescriptor.FromProperty(Button.IsEnabledProperty, typeof(Button));
            isEnabledButtonDpd.AddValueChanged(this.quitGameButton, (s, e) =>
                {
                    if (this.quitGameButton.IsEnabled)
                    {
                        ComputeButtonsVisibilityAndFocus();
                    }
                });

            DependencyPropertyDescriptor isKeyboardFocusedPropertyTextBoxDpd = DependencyPropertyDescriptor.FromProperty(TextBox.IsKeyboardFocusedProperty, typeof(TextBox));
            isKeyboardFocusedPropertyTextBoxDpd.AddValueChanged(this.OpponentWord1TextBox, TextBox_GotKeyboardFocus);
            isKeyboardFocusedPropertyTextBoxDpd.AddValueChanged(this.OpponentWord2TextBox, TextBox_GotKeyboardFocus);
            isKeyboardFocusedPropertyTextBoxDpd.AddValueChanged(this.OpponentWord3TextBox, TextBox_GotKeyboardFocus);
            isKeyboardFocusedPropertyTextBoxDpd.AddValueChanged(this.OpponentWord4TextBox, TextBox_GotKeyboardFocus);
            isKeyboardFocusedPropertyTextBoxDpd.AddValueChanged(this.OpponentWord5TextBox, TextBox_GotKeyboardFocus);

        }

        /// <summary>
        /// Obtient le ViewModel associé
        /// </summary>
        [ViewModelImport(Modules.Game)]
        public object ViewModel
        {
            get { return this.DataContext; }
            set { this.DataContext = value; }
        }
        
        private void TextBox_GotKeyboardFocus(object sender, EventArgs e)
        {
            TextBox textBox = sender as TextBox;

            if (object.ReferenceEquals(textBox, null))
            {
                throw new ArgumentNullException("sender");
            }

            if (textBox.IsKeyboardFocused)
            {
                if (this._newRound)
                {
                    this._newRound = false;
                }
                else
                {
                    Tuple<TextBox, Button> tuple = this._opponentWordsControls.Single(t => object.ReferenceEquals(t.Item1, sender));
                    this._selectedIndex = this._opponentWordsControls.IndexOf(tuple);
                }
                this.ComputeButtonsVisibilityAndFocus();
            }
        }

        private void ComputeButtonsVisibilityAndFocus()
        {
            int j = this._selectedIndex;
            bool done = false;

            for (int i = 0; i < this._opponentWordsControls.Count; i++)
            {
                if (j == this._opponentWordsControls.Count) 
                {
                    j = 0;
                }

                Tuple<TextBox, Button> tuple = this._opponentWordsControls[j];
                
                if (tuple.Item1.IsEnabled && !done)
                {
                    if (!tuple.Item1.IsKeyboardFocused)
                    {
                        Keyboard.Focus(tuple.Item1);
                    }
                    tuple.Item2.Visibility = Visibility.Visible;
                    tuple.Item2.IsDefault = true;
                    done = true;
                }
                else
                {
                    tuple.Item2.Visibility = Visibility.Hidden;
                    tuple.Item2.IsDefault = false;
                }

                j++;
			}
        }
    }
}
